<template>
  <view class="points-mall-container">
    <!-- 顶部导航栏 -->
    <view class="header">
      <view class="placeholder"></view>
      <view class="page-title">积分商城</view>
      <view class="placeholder"></view>
    </view>

    <!-- 积分卡片 -->
    <view class="points-card">
      <view class="points-info">
        <view class="points-label">我的积分</view>
        <view class="points-value">{{ userPoints }}</view>
      </view>
      <view class="points-detail-btn" @click="navigateToPointsDetail">
        积分明细
      </view>
    </view>
    <!-- 积分兑换人民币卡片 -->
    <view class="exchange-card">
      <view class="exchange-title">积分兑换人民币</view>
      <view class="exchange-rate">当前兑换比例: 100积分 = 1元</view>
      <view class="exchange-inputs">
        <view class="input-item">
          <input type="number" v-model="exchangePoints" placeholder="输入要兑换的" />
          <text class="input-label">积分</text>
        </view>
        <text class="exchange-arrow">→</text>
        <view class="input-item">
          <input type="text" :value="calculateMoney" disabled />
          <text class="input-label">元</text>
        </view>
      </view>
      <button class="exchange-money-btn" @click="exchangeMoney">确认兑换</button>
    </view>

    <!-- 搜索栏 -->
    <view class="search-bar">
      <view class="search-input">
        <text class="iconfont icon-search"></text>
        <input type="text" placeholder="搜索商品" v-model="searchKeyword" />
      </view>
      <view class="filter-btn">
        <text class="iconfont icon-filter"></text>
      </view>
    </view>

    <!-- 分类标签 -->
    <view class="category-tabs">
      <view v-for="(category, index) in categories" :key="index" class="category-tab"
        :class="{ active: currentCategory === category.id }" @click="selectCategory(category.id)">
        {{ category.name }}
      </view>
    </view>

    <!-- 商品列表 -->
    <view class="goods-grid">
      <view class="goods-item" v-for="(item, index) in filteredGoods" :key="index" @click="showGoodsDetail(item)">
        <view class="goods-image">
          <image :src="item.image || '/static/images/placeholder.svg'" mode="aspectFill"></image>
        </view>
        <view class="goods-name">{{ item.name }}</view>
        <view class="goods-points">{{ item.points }} 积分</view>
        <view class="exchange-btn" @click.stop="exchangeGoods(item)">兑换</view>
      </view>
    </view>


  </view>
</template>

<script>
export default {
  data() {
    return {
      userPoints: 1250,
      searchKeyword: '',
      currentCategory: 0, // 0表示全部
      exchangePoints: '',
      categories: [
        { id: 0, name: '全部' },
        { id: 1, name: '日用品' },
        { id: 2, name: '服饰' },
        { id: 3, name: '食品' }
      ],
      goodsList: [
        {
          id: 1,
          name: '环保购物袋',
          points: 100,
          category: 1,
          image: '/static/images/goods/default.png',
          description: '环保材质，可反复使用的购物袋'
        },
        {
          id: 2,
          name: '有机棉T恤',
          points: 500,
          category: 2,
          image: '/static/images/goods/default.png',
          description: '100%有机棉制成，舒适透气'
        },
        {
          id: 3,
          name: '不锈钢保温杯',
          points: 300,
          category: 1,
          image: '/static/images/goods/default.png',
          description: '304不锈钢材质，保温效果好'
        },
        {
          id: 4,
          name: '竹制餐具套装',
          points: 250,
          category: 1,
          image: '/static/images/goods/default.png',
          description: '天然竹材质，环保健康'
        },
        {
          id: 5,
          name: '有机棉袜子',
          points: 150,
          category: 2,
          image: '/static/images/goods/default.png',
          description: '有机棉材质，舒适透气'
        },
        {
          id: 6,
          name: '环保洗衣液',
          points: 200,
          category: 1,
          image: '/static/images/goods/default.png',
          description: '植物配方，温和不刺激'
        }
      ]
    };
  },
  computed: {
    filteredGoods() {
      let result = this.goodsList;

      // 分类筛选
      if (this.currentCategory !== 0) {
        result = result.filter(item => item.category === this.currentCategory);
      }

      // 关键词搜索
      if (this.searchKeyword) {
        const keyword = this.searchKeyword.toLowerCase();
        result = result.filter(item =>
          item.name.toLowerCase().includes(keyword) ||
          item.description.toLowerCase().includes(keyword)
        );
      }

      return result;
    },
    calculateMoney() {
      if (!this.exchangePoints || isNaN(parseInt(this.exchangePoints))) {
        return '0.00';
      }
      const money = (parseInt(this.exchangePoints) / 100).toFixed(2);
      return money;
    }
  },
  methods: {
    navigateToPointsDetail() {
      uni.navigateTo({
        url: '/pages/points/detail'
      });
    },

    selectCategory(categoryId) {
      this.currentCategory = categoryId;
    },

    showGoodsDetail(item) {
      uni.navigateTo({
        url: `/pages/points/goods-detail?id=${item.id}`
      });
    },

    exchangeGoods(item) {
      // 检查积分是否足够
      if (this.userPoints < item.points) {
        uni.showToast({
          title: '积分不足',
          icon: 'none'
        });
        return;
      }

      // 弹窗确认兑换
      uni.showModal({
        title: '确认兑换',
        content: `确定使用${item.points}积分兑换"${item.name}"吗？`,
        success: (res) => {
          if (res.confirm) {
            // 模拟兑换过程
            uni.showLoading({
              title: '兑换中...'
            });

            setTimeout(() => {
              uni.hideLoading();
              // 扣减积分
              this.userPoints -= item.points;

              uni.showToast({
                title: '兑换成功',
                icon: 'success'
              });

              // 跳转到兑换成功页面
              setTimeout(() => {
                uni.navigateTo({
                  url: `/pages/points/exchange-success?id=${item.id}`
                });
              }, 1500);
            }, 2000);
          }
        }
      });
    },

    exchangeMoney() {
      const points = parseInt(this.exchangePoints);

      // 校验输入
      if (!points || isNaN(points)) {
        uni.showToast({
          title: '请输入有效的积分数量',
          icon: 'none'
        });
        return;
      }

      // 检查积分是否足够
      if (this.userPoints < points) {
        uni.showToast({
          title: '积分不足',
          icon: 'none'
        });
        return;
      }

      // 检查最低兑换数量
      if (points < 100) {
        uni.showToast({
          title: '最低兑换100积分',
          icon: 'none'
        });
        return;
      }

      // 弹窗确认兑换
      uni.showModal({
        title: '确认兑换',
        content: `确定将${points}积分兑换为${this.calculateMoney}元人民币吗？`,
        success: (res) => {
          if (res.confirm) {
            // 模拟兑换过程
            uni.showLoading({
              title: '兑换中...'
            });

            setTimeout(() => {
              uni.hideLoading();
              // 扣减积分
              this.userPoints -= points;
              // 清空输入
              this.exchangePoints = '';

              uni.showToast({
                title: '兑换成功',
                icon: 'success'
              });
            }, 2000);
          }
        }
      });
    }
  }
};
</script>

<style lang="scss">
@import '../../styles/global.scss';

.points-mall-container {
  padding-bottom: 40rpx;
  background-color: $bg-color;
  min-height: 100vh;
}

/* 顶部导航 */
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20rpx 30rpx;
  background-color: $card-bg;
  position: relative;
  border-bottom: 1rpx solid $border-light;

  .placeholder {
    width: 60rpx;
  }

  .page-title {
    font-size: 36rpx;
    font-weight: bold;
    color: $text-black;
  }
}

/* 积分卡片 */
.points-card {
  margin: 20rpx;
  padding: 30rpx;
  border-radius: 16rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: $primary-green;
  color: $text-white;
  box-shadow: $card-shadow;

  .points-info {
    .points-label {
      font-size: 28rpx;
      margin-bottom: 10rpx;
    }

    .points-value {
      font-size: 56rpx;
      font-weight: bold;
    }
  }

  .points-detail-btn {
    padding: 10rpx 30rpx;
    background-color: rgba(255, 255, 255, 0.2);
    border-radius: 30rpx;
    font-size: 26rpx;
  }
}

/* 搜索栏 */
.search-bar {
  margin: 20rpx;
  display: flex;
  align-items: center;

  .search-input {
    flex: 1;
    display: flex;
    align-items: center;
    background-color: $card-bg;
    padding: 16rpx 24rpx;
    border-radius: 40rpx;
    margin-right: 20rpx;

    .iconfont {
      margin-right: 16rpx;
      color: $text-light-gray;
    }

    input {
      flex: 1;
      height: 60rpx;
      font-size: 28rpx;
    }
  }

  .filter-btn {
    width: 80rpx;
    height: 80rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: $card-bg;
    border-radius: 40rpx;

    .iconfont {
      font-size: 36rpx;
      color: $text-gray;
    }
  }
}

/* 分类标签 */
.category-tabs {
  display: flex;
  padding: 0 10rpx;
  margin-bottom: 20rpx;
  white-space: nowrap;
  overflow-x: auto;

  &::-webkit-scrollbar {
    display: none;
  }

  .category-tab {
    padding: 12rpx 30rpx;
    margin: 0 10rpx;
    font-size: 28rpx;
    color: $text-gray;
    background-color: $card-bg;
    border-radius: 30rpx;

    &.active {
      color: $primary-green;
      font-weight: bold;
      background-color: $light-green;
    }
  }
}

/* 商品列表 */
.goods-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20rpx;
  padding: 0 20rpx;
  margin-bottom: 40rpx;

  .goods-item {
    background-color: $card-bg;
    border-radius: 16rpx;
    overflow: hidden;
    box-shadow: $card-shadow;
    display: flex;
    flex-direction: column;

    .goods-image {
      width: 100%;
      height: 240rpx;
      background-color: #f0f0f0;

      image {
        width: 100%;
        height: 100%;
      }
    }

    .goods-name {
      padding: 16rpx 20rpx 10rpx;
      font-size: 28rpx;
      font-weight: bold;
      color: $text-black;
    }

    .goods-points {
      padding: 0 20rpx 16rpx;
      font-size: 26rpx;
      color: $primary-green;
      font-weight: bold;
    }

    .exchange-btn {
      margin: 0rpx 20rpx 20rpx;
      background-color: $primary-green;
      color: $text-white;
      text-align: center;
      padding: 12rpx 0;
      border-radius: 30rpx;
      font-size: 26rpx;
    }
  }
}

/* 积分兑换人民币卡片 */
.exchange-card {
  margin: 20rpx;
  padding: 30rpx;
  background-color: $card-bg;
  border-radius: 16rpx;
  box-shadow: $card-shadow;

  .exchange-title {
    font-size: 32rpx;
    font-weight: bold;
    color: $text-black;
    margin-bottom: 16rpx;
  }

  .exchange-rate {
    font-size: 26rpx;
    color: $text-light-gray;
    margin-bottom: 30rpx;
  }

  .exchange-inputs {
    display: flex;
    align-items: center;
    margin-bottom: 30rpx;

    .input-item {
      flex: 1;
      display: flex;
      align-items: center;
      background-color: $bg-color;
      padding: 16rpx 20rpx;
      border-radius: 8rpx;

      input {
        flex: 1;
        height: 60rpx;
        font-size: 30rpx;
      }

      .input-label {
        font-size: 28rpx;
        color: $text-gray;
        margin-left: 10rpx;
      }
    }

    .exchange-arrow {
      margin: 0 20rpx;
      color: $text-gray;
      font-size: 36rpx;
    }
  }

  .exchange-money-btn {
    background-color: $primary-green;
    color: $text-white;
    font-size: 30rpx;
    padding: 20rpx 0;
    border-radius: 40rpx;
    text-align: center;
    margin: 0;
  }
}
</style>